﻿'Description:排产-有限产能排产类
'Copyright (c) : 通力凯顿（北京）系统集成有限公司
'Writer:Yuxj 
'create Date:20220720
'Rewriter:
'Rewrite Date:

Imports DatabaseAccess.DBUtility
Imports BusinessRule.DTO
Imports System.Text
Imports External.APICalling

Public Class ApsZsShiftAndTeamBusiness
    ''' <summary>
    ''' 获取班和日历关联信息
    ''' </summary>
    ''' <param name="para"></param>
    ''' <returns>Yuxj</returns>
    ''' <remarks>20220720</remarks>
    Public Function GetApsShiftAndWorkshift(ByVal para As Dictionary(Of String, String)) As DataTable
        Dim strSql As String =
            <string>
                select f.factoryname,w.workcentername,s.shiftname,cd.workshiftname,s.workcenterid,s.shiftid,s.workshiftid from shift s
                left join workcenter w on w.workcenterid=s.workcenterid
                left join factory f on s.factoryid=f.factoryid                
                left join workshift cd on cd.workshiftid=s.workshiftid
                where 1=1
            </string>

        If para.ContainsKey("FactoryID") Then
            strSql += String.Format(" and f.FactoryID = '{0}' ", para("FactoryID"))
        End If

        If para.ContainsKey("WorkcenterID") Then
            strSql += String.Format(" and s.workcenterid = '{0}' ", para("WorkcenterID"))
        End If

        Dim result As DataTable = DatabaseHelper.Query_ReturnDatTable(strSql)
        Return result
    End Function

    ''' <summary>
    ''' 更新班 绑定的日历信息
    ''' </summary>
    ''' <param name="para"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function BindShiftAndWorkshift(ByVal para As Dictionary(Of String, String)) As Integer
        Dim strSql As String = ""
        If (para.ContainsKey("WorkshiftID")) Then
            strSql =
                String.Format("update shift s set s.workshiftid='{0}' where s.shiftid='{1}'", para("WorkshiftID"), para("ShiftID"))
        Else
            strSql =
                String.Format("update shift s set s.workshiftid=null where s.shiftid='{0}'", para("ShiftID")) '清除功能
        End If

        Return DatabaseHelper.ExecuteSql(strSql)

    End Function

    ''' <summary>
    ''' 获取组和分厂，工区，班，版次关联信息
    ''' </summary>
    ''' <param name="para"></param>
    ''' <returns>Yuxj</returns>
    ''' <remarks>20220720</remarks>
    Public Function GetApsShiftAndTeamRelation(ByVal para As Dictionary(Of String, String)) As DataTable
        Dim strSql As String =
            <string>
                select f.factoryname,w.workcentername,s.shiftname,t.teamname,ws.workshiftname,
                       f.factoryid,w.workcenterid,s.shiftid,t.teamid,ws.workshiftid
                 from team t
                left join shift s on t.shiftid=s.shiftid
                left join workcenter w on w.workcenterid=t.workcenterid
                left join factory f on t.factoryid=f.factoryid  
                left join workshift ws on ws.workshiftid=t.workshiftid
                where   1=1
            </string>

        If para.ContainsKey("FactoryID") Then
            strSql += String.Format(" and t.FactoryID = '{0}' ", para("FactoryID"))
        End If

        If para.ContainsKey("WorkcenterID") Then
            strSql += String.Format(" and t.workcenterid = '{0}' ", para("WorkcenterID"))
        End If

        If para.ContainsKey("ShiftID") Then
            strSql += String.Format(" and t.ShiftID = '{0}' ", para("ShiftID"))
        End If

        If para.ContainsKey("TeamID") Then
            strSql += String.Format(" and t.TeamID = '{0}' ", para("TeamID"))
        End If

        Dim result As DataTable = DatabaseHelper.Query_ReturnDatTable(strSql)
        Return result
    End Function

    ''' <summary>
    ''' 更新组 绑定的班次信息
    ''' </summary>
    ''' <param name="para"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function BindTeamAndWorkshift(ByVal para As Dictionary(Of String, String)) As Integer
        Dim strSql As String = ""
        If (para.ContainsKey("WorkshiftID")) Then
            strSql = String.Format("update team s set s.workshiftid='{0}' where s.teamid='{1}'", para("WorkshiftID"), para("TeamID"))
        Else
            strSql = String.Format("update team s set s.workshiftid=null where s.teamid='{0}'", para("TeamID")) '清除功能
        End If
        Return DatabaseHelper.ExecuteSql(strSql)
    End Function

    ''' <summary>
    ''' 更新组 的人员信息
    ''' </summary>
    ''' <param name="para"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function BindTeamEmployee(ByVal para As Dictionary(Of String, String), ByVal dt As DataTable) As Boolean
        BindTeamEmployee = False
        Dim sqlList As New ArrayList
        Dim strSql As String = ""
        strSql = String.Format("delete from teamemployee t where t.teamid='{0}'", para("TeamID")) '清除功能
        sqlList.Add(strSql)
        Dim seq As Integer = 0
        For Each dr As DataRow In dt.Rows
            Dim employeeid As String = dr("employeeid").ToString
            seq += 1
            strSql = String.Format("insert into teamemployee(teamid,employeeid,fieldid,sequence) values('{0}','{1}','671088848',{2}) ", para("TeamID"), employeeid, seq)
            sqlList.Add(strSql)
        Next
        DatabaseHelper.ExecuteSqlTran(sqlList)
        Return True
    End Function
    ''' <summary>
    ''' 获取--班组关联的人员信息
    ''' </summary>
    ''' <param name="para"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function GetTeamEmployees(ByVal para As Dictionary(Of String, String)) As DataTable
        Dim strsql As New StringBuilder
        strsql.AppendLine(<string>
                        SELECT e2.EMPLOYEEID,e2.EMPLOYEENAME,e2.FULLNAME FROM teamemployee e
                            LEFT JOIN EMPLOYEE e2 ON e2.EMPLOYEEID =e.EMPLOYEEID 
                          </string>)
        strsql.AppendFormat("WHERE FIELDID='671088848'  AND TEAMID='{0}'", para("TeamID"))
        Return DatabaseHelper.Query_ReturnDatTable(strsql.ToString)
    End Function

End Class
